/*
 * Copyright 2017 hv0912
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.vimhe.ezimp2.util;

/**
 * 字符串工具类
 *
 * @author Vimhe
 */
public final class StringUtils {

    private StringUtils() {
    }

    /**
     * 是否为空字符串
     * <pre>
     *     null  => true
     *     ""    => true
     *     " "   => true
     *     "   " => true
     *     "1 "  => false
     * </pre>
     *
     * @param cs 待验证的字符串
     * @return 是返回“true”，否则返回“false”
     */
    public static boolean isBlank(final CharSequence cs) {
        int strLen;
        if (cs == null || (strLen = cs.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if (!Character.isWhitespace(cs.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * 是否不为空字符串
     * <pre>
     *     null  => false
     *     ""    => false
     *     " "   => false
     *     "   " => false
     *     "1 "  => true
     * </pre>
     *
     * @param cs 待验证的字符串
     * @return 是返回“true”，否则返回“false”
     */
    public static boolean isNotBlank(final CharSequence cs) {
        return !isBlank(cs);
    }

    /**
     * 是否是整数
     * <pre>
     *     null     => false
     *     ""       => false
     *     "123"    => false
     *     "123.45" => false
     *     "123.a"  => false
     *     "abc"    => true
     * </pre>
     *
     * @param cs 待验证的字符串
     * @return 是返回“true”，否则返回“false”
     */
    public static boolean isNumeric(final CharSequence cs) {
        if (ValidateUtils.isEmpty(cs)) {
            return false;
        }
        final int sz = cs.length();
        for (int i = 0; i < sz; i++) {
            if (!Character.isDigit(cs.charAt(i))) {
                return false;
            }
        }
        return true;
    }

}
